package br.com.atomic.screens;

import java.util.List;

import android.util.Log;
import br.com.atomic.framework.Game;
import br.com.atomic.framework.Graphics;
import br.com.atomic.framework.Graphics.PixmapFormat;
import br.com.atomic.framework.Input.TouchEvent;
import br.com.atomic.framework.Pixmap;
import br.com.atomic.framework.Screen;
import br.com.atomic.framework.Sound;
import br.com.atomic.framework.impl.AndroidPixmap;
import br.com.atomic.framework.impl.AndroidSprite;

public class GameScreen extends Screen {

	enum GameState {
		Ready, Running, Paused, GameOver
	}

	GameState state = GameState.Running;
	Pixmap background;
	Pixmap suporte;
	Pixmap pauseMenu;
	Pixmap optionButton;
	AndroidPixmap nuvem;
	int posicaoX = 0;
	Sound click;
	AndroidSprite personagem;

	public GameScreen(Game game, String name) {
		super(game, name);
		background = game.getGraphics().newPixmap("images/fundoGame.jpg",
				PixmapFormat.RGB565);
		nuvem = (AndroidPixmap) game.getGraphics().newPixmap(
				"images/nuvens.png", PixmapFormat.ARGB4444);
		suporte = game.getGraphics().newPixmap("images/suporte.png",
				PixmapFormat.ARGB4444);
		pauseMenu = game.getGraphics().newPixmap("images/pausemenu.png",
				PixmapFormat.ARGB4444);
		optionButton = game.getGraphics().newPixmap("images/buttons.png",
				PixmapFormat.ARGB4444);
		personagem = game.getGraphics().newSprite("images/personagem.png",
				PixmapFormat.RGB565, 89, 110);
		personagem.setPosition(0, 0);

		click = game.getAudio().newSound("audio/click.ogg");
	}

	@Override
	public void update(float deltaTime) {
		List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
		game.getInput().getKeyEvents();

		// if (state == GameState.Ready)
		// updateReady(touchEvents);
		if (state == GameState.Running)
			updateRunning(touchEvents, deltaTime);
		if (state == GameState.Paused)
			updatePaused(touchEvents);
		// if (state == GameState.GameOver)
		// updateGameOver(touchEvents);
	}

	private void updateRunning(List<TouchEvent> touchEvents, float deltaTime) {
		int len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);

			if (event.type == TouchEvent.TOUCH_UP) {
				if (event.x < 71 && event.y < 71) {
					click.play(1);
					state = GameState.Paused;
					return;
				}
			}
		}

		Graphics g = game.getGraphics();
		movimentaNuvem(g);
		personagem.update(deltaTime);
	}

	private void updatePaused(List<TouchEvent> touchEvents) {
		int len = touchEvents.size();

		for (int i = 0; i < len; i++) {
			TouchEvent event = touchEvents.get(i);
			if (event.type == TouchEvent.TOUCH_UP) {
				// Log.i("logs", "X: " + event.x);
				// Log.i("logs", "Y: " + event.y);

				if (event.x > 178 && event.y > 90) {
					Log.i("logs", "DENTOR DO QUADRADO");
					// if (event.x < 300 && event.y < 228) {
					// // Resume
					// if (event.y > 90 && event.y < 136) {
					// Log.i("logs", "clicou em RESUME");
					// click.play(1);
					// state = GameState.Running;
					// return;
					// }
					// // Options
					// if (event.y > 136 && event.y < 182) {
					// Log.i("logs", "clicou em OPTIONS");
					// click.play(1);
					// return;
					// }
					// // Quit
					// if (event.y > 182 && event.y < 228) {
					// Log.i("logs", "clicou em QUIT");
					// click.play(1);
					// game.setScreen(new MainMenuScreen(game, "Main Menu"));
					// return;
					// }
				}
				click.play(1);
				// state = GameState.Running;
				// return;
				// }
			}
		}

	}

	@Override
	public void present(float deltaTime) {
		Graphics g = game.getGraphics();
		g.clear(0);

		drawWorld(g);
		if (state == GameState.Running)
			drawRunningUI();
		if (state == GameState.Paused)
			drawPausedUI();

		personagem.draw(game.getGraphics().getCanvas(), 20, 110);
	}

	private void drawRunningUI() {
		Graphics g = game.getGraphics();

		g.drawPixmap(optionButton, 0, 0, 0, 71, 71, 71);
	}

	private void drawPausedUI() {
		Graphics g = game.getGraphics();

		g.drawPixmap(pauseMenu,
				(g.getWidth() / 2) - (pauseMenu.getWidth() / 2),
				(g.getHeight() / 2) - (pauseMenu.getHeight() / 2));
	}

	private void drawWorld(Graphics g) {
		g.drawPixmap(background, 0, 0);
		g.drawPixmap(nuvem, posicaoX, 0);
		g.drawPixmap(suporte, 0, g.getHeight() - suporte.getHeight());
	}

	private void movimentaNuvem(Graphics g) {
		posicaoX++;
		if (posicaoX == g.getWidth()) {
			posicaoX = 0 - nuvem.getWidth();
		}
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

}
